package data;


/**
 * describes a recommendation record
 * @author the secret 4 and the dog
 *
 */
public class RatingTuple{

	private MovieTuple _movie;
	private UserTuple _user;
	private String _username;
	private String _moviename;
	private String _date;
	private int _rating;
	private String _description;

	/**
	 * creates a new instance of RecTuple
	 * @param movie
	 * @param user
	 * @param date
	 * @param rating
	 * @param description
	 */
	private RatingTuple(MovieTuple movie, UserTuple user, String date, String rating, String description) {
		_movie = movie;
		_user = user;
		_date = date;
		_rating = Integer.parseInt(rating);
		_description = description;
	}
	
	private RatingTuple(MovieTuple movie, String user, String date, String rating, String description) {
		_movie = movie;
		_user = null;
		_username = user;
		_date = date;
		_rating = Integer.parseInt(rating);
		_description = description;
	}
	
	private RatingTuple(String movie, UserTuple user, String date, String rating, String description) {
		_movie = null;		
		_moviename = movie;
		_user = user;
		_date = date;
		_rating = Integer.parseInt(rating);
		_description = description;
	}
	
	private RatingTuple(String movie, String user, String date, String rating, String description) {
		_movie = null;
		_user = null;
		_moviename = movie;
		_username = user;
		_date = date;
		_rating = Integer.parseInt(rating);
		_description = description;
	}

	public static RatingTuple createNew(MovieTuple movie, UserTuple user, String date, String rating, String description) {
		RatingTuple tmp = new RatingTuple(movie, user, date, rating, description);
		movie.addRating(tmp);
		user.addRating(tmp);
		return tmp;
	}
	
	public static RatingTuple createNew(MovieTuple movie, String user, String date, String rating, String description) {
		RatingTuple tmp = new RatingTuple(movie, user, date, rating, description);
		movie.addExistingRating(tmp);
		//user.addRating(tmp);
		return tmp;
	}
	
	public static RatingTuple createNew(String movie, UserTuple user, String date, String rating, String description) {
		RatingTuple tmp = new RatingTuple(movie, user, date, rating, description);
		//movie.addRating(tmp);
		user.addRating(tmp);
		return tmp;
	}
	
	public static RatingTuple createNew(String movie, String user, String date, String rating, String description) {
		RatingTuple tmp = new RatingTuple(movie, user, date, rating, description);
		//movie.addRating(tmp);
		//user.addRating(tmp);
		return tmp;
	}

	public boolean equals(Object other ){
		boolean ans=true;
		if (!_movie.equals(((RatingTuple )other)._movie)){
			ans=false;
		}
		if (!_user.equals(((RatingTuple )other)._user)){
			ans=false;
		}
		return ans;
	}

	public String getKey(){
		return (_user!=null ? _user.get_name() : _username)+"%"+(_movie!=null ? _movie.get_name() : _moviename);
	}
	public String toString(){
		return "Movie Name: "+_movie+"\nRecommender: "+_user+"\nDate: "+_date+"\nRating: "+_rating+"\nDescription: "+_description+"\n";
	}
	public MovieTuple get_movie() {
		if (_movie == null){
			_movie = SystemData.getInstance().getMovie(_moviename);
		}
		return _movie;
	}
	
	public UserTuple get_user() {
		if (_user == null){
			_user = SystemData.getInstance().getUser(_username);
		}
		return _user;
	}
	public String get_date() {
		return _date;
	}
	public int get_rating() {
		return _rating;
	}
	
	public String get_description() {
		return _description;
	}

	public void edit(String description, String rate, String date) {
		_description = description;
		_rating = Integer.parseInt(rate);
		_date = date;
	}
	
//	public static String calcKey(UserTuple user,MovieTuple movie ){
//		return user.get_name()+"%"+movie.get_name();
//	}
}
